<template>
  <el-dialog v-if="uploadModal" v-model="uploadModal" :title="translateTitle('Dams.ApplicationInterface.导入数据')" width="80%" @close="dialogClose">
    <el-steps :active="active" align-center>
      <el-step :title="translateTitle('Dams.ApplicationInterface.上传模版数据')" />
      <el-step v-if="dataDetection" :title="translateTitle('Dams.ApplicationInterface.模版数据检测')" />
    </el-steps>
    <div>
      <div class="hint_">
        <p class="hint-title">{{ translateTitle('Dams.ApplicationInterface.特别提示') }}</p>
        <p class="hint-explain">{{ translateTitle('Dams.ApplicationInterface.请使用系统要求模板按格式要求填写数据') }}</p>
      </div>
      <div class="box_">
        <div class="left-box"></div>
        <div>
          <p>{{ translateTitle('Dams.ApplicationInterface.填写导入数据信息') }}</p>
          <p>{{ translateTitle('Dams.ApplicationInterface.请按照数据模板的格式准备导入数据') }}</p>
          <el-button type="text" @click="download">{{ translateTitle('Dams.ApplicationInterface.下载模板') }}</el-button>
        </div>
      </div>
      <div class="box_">
        <div class="left-box"></div>
        <vui-upload
          ref="fileUploadRef"
          class="text-left"
          :file-name="uploadOptions.name"
          :limit="1"
          :post-data="uploadOptions.postData"
          :url="uploadOptions.url"
          @uploadSuccess="uploadSuccess"
        >
          <div>
            <p>{{ translateTitle('Dams.ApplicationInterface.上传填好的信息表') }}</p>
            <p>{{ translateTitle('Dams.ApplicationInterface.文件后缀名必须为xls或xlsx') }}</p>
            <el-button size="small" type="text">{{ translateTitle('Dams.ApplicationInterface.上传文件') }}</el-button>
          </div>
        </vui-upload>
      </div>
      <div class="footer_">
        <el-button type="primary" @click="clickUpload">
          {{ dataDetection ? translateTitle('Dams.ApplicationInterface.下一步') : translateTitle('Dams.ApplicationInterface.确认') }}
        </el-button>
      </div>
    </div>
  </el-dialog>
</template>
<script>
  import { GFCD } from '@/utils/utils'
  import { download } from '../api/home'
  import { translateTitle } from '@/utils/i18n'
  const configSetting = GFCD('dams')
  const downloadList = [
    {
      '#EXPORT_WHERE#': {
        TEMPLATE_ID: 'sys_cust_org',
        FIELD_ID: 'MESSAGE_GROUP_ID,MESSAGE_GROUP_NAME,ACTIVE_FLAG',
      },
      '#EXPORT_SQL_ID#': 'apim.common.temp.export',
      '#EXPORT_API_NAME#': 'apim.common.temp.export',
      '#QUERY_SORT#': {
        SHOW_SEQUENCE: 'ASC',
      },
      '#EXPORT_FILE_NAME#': '',
      '#EXPORT_FILE_TYPE#': 'xlsx',
      '#EXPORT_SHEET_NAME#': 'sys_cust_org',
    },
    {
      '#EXPORT_WHERE#': {
        TEMPLATE_ID: 'sys_message_config',
        FIELD_ID:
          'MESSAGE_GROUP_ID,MESSAGE_ID,ACTIVE_FLAG,STRUCTURE_TYPE,DATA_ACTION,DATA_JNDI_NAME,DATA_USER_CLASS,DATA_PARAM1,DATA_PARAM2,DATA_PARAM3,DATA_PARAM4,DATA_PARAM5,OUT_DATA_ACTION,OUT_JNDI_NAME,OUT_USER_CLASS,RESEND_CONFIG,HTTP_CONFIG,OUT_PARAM1,OUT_PARAM2,OUT_PARAM3,OUT_PARAM4,OUT_PARAM5,RETURN_MESSAGE_ID,RESPONSE_TYPE,RESPONSE_BEAN,RESPONSE_MODEL,SOAP_VERSION,LOG_EXPIRE_TIME,GET_DOC_NO_TYPE,DATA_PARAM,BATCH_FLAG,IDU_FLAG,RETURN_CONDITION,INTERFACE_DESC,ASSOCIATION_SYSTEM,RETURN_PARAM1,RETURN_PARAM2,RETURN_PARAM3,RETURN_PARAM4,RETURN_PARAM5',
      },
      '#EXPORT_SQL_ID#': 'apim.common.temp.export',
      '#EXPORT_API_NAME#': 'apim.common.temp.export',
      '#QUERY_SORT#': {
        SHOW_SEQUENCE: 'ASC',
      },
      '#EXPORT_FILE_NAME#': '',
      '#EXPORT_FILE_TYPE#': 'xlsx',
      '#EXPORT_SHEET_NAME#': 'sys_message_config',
    },
    {
      '#EXPORT_WHERE#': {
        TEMPLATE_ID: 'sys_message_config_value',
        FIELD_ID:
          'MESSAGE_GROUP_ID,MESSAGE_ID,SN,ACTIVE_FLAG,TYPE,VALUE_STR01,VALUE_STR02,VALUE_STR03,VALUE_STR04,VALUE_STR05,VALUE_STR06,VALUE_STR07,VALUE_STR08,VALUE_STR09,VALUE_STR10',
      },
      '#EXPORT_SQL_ID#': 'apim.common.temp.export',
      '#EXPORT_API_NAME#': 'apim.common.temp.export',
      '#QUERY_SORT#': {
        SHOW_SEQUENCE: 'ASC',
      },
      '#EXPORT_FILE_NAME#': '',
      '#EXPORT_FILE_TYPE#': 'xlsx',
      '#EXPORT_SHEET_NAME#': 'sys_message_config_value',
    },
    {
      '#EXPORT_WHERE#': {
        TEMPLATE_ID: 'sys_message_field_mapping',
        FIELD_ID:
          'MESSAGE_GROUP_ID,MESSAGE_ID,ACTIVE_FLAG,MESSAGE_TYPE,STD_FIELD,WEB_FIELD,WEB_DATA_TYPE,WEB_DATA_WIDTH,WEB_DATA_FORMAT,WEB_DEFAULT,KEY_FLAG,UPDATE_FLAG',
      },
      '#EXPORT_SQL_ID#': 'apim.common.temp.export',
      '#EXPORT_API_NAME#': 'apim.common.temp.export',
      '#QUERY_SORT#': {
        SHOW_SEQUENCE: 'ASC',
      },
      '#EXPORT_FILE_NAME#': '',
      '#EXPORT_FILE_TYPE#': 'xlsx',
      '#EXPORT_SHEET_NAME#': 'sys_message_field_mapping',
    },
    {
      '#EXPORT_WHERE#': {
        TEMPLATE_ID: 'sys_message_field_xml',
        FIELD_ID:
          'MESSAGE_GROUP_ID,MESSAGE_ID,ACTIVE_FLAG,MESSAGE_TYPE,ADAPTER_TYPE,XPATH_EXPRESS,STD_FIELD,ALLOW_EMPTY_FLAG,IS_TRANSFER_FI,LEDDATA_TYPE,DATA_WIDTH,NAME_SPACE,PARENT_ELEMENT,NODE_VALUE',
      },
      '#EXPORT_SQL_ID#': 'apim.common.temp.export',
      '#EXPORT_API_NAME#': 'apim.common.temp.export',
      '#QUERY_SORT#': {
        SHOW_SEQUENCE: 'ASC',
      },
      '#EXPORT_FILE_NAME#': '',
      '#EXPORT_FILE_TYPE#': 'xlsx',
      '#EXPORT_SHEET_NAME#': 'sys_message_field_xml',
    },
    {
      '#EXPORT_WHERE#': {
        TEMPLATE_ID: 'sys_message_sql',
        FIELD_ID: 'MESSAGE_GROUP_ID,MESSAGE_ID,ACTIVE_FLAG,MESSAGE_TYPE,SN,SQLTEXT',
      },
      '#EXPORT_SQL_ID#': 'apim.common.temp.export',
      '#EXPORT_API_NAME#': 'apim.common.temp.export',
      '#QUERY_SORT#': {
        SHOW_SEQUENCE: 'ASC',
      },
      '#EXPORT_FILE_NAME#': '',
      '#EXPORT_FILE_TYPE#': 'xlsx',
      '#EXPORT_SHEET_NAME#': 'sys_message_sql',
    },
    {
      '#EXPORT_WHERE#': {
        TEMPLATE_ID: 'sys_message_structure',
        FIELD_ID: 'MESSAGE_GROUP_ID,MESSAGE_ID,ACTIVE_FLAG,MESSAGE_TYPE,ADAPTER_TYPE,XPATH_EXPRESS,PARENT_ELEMENT,DATA_SQL',
      },
      '#EXPORT_SQL_ID#': 'apim.common.temp.export',
      '#EXPORT_API_NAME#': 'apim.common.temp.export',
      '#QUERY_SORT#': {
        SHOW_SEQUENCE: 'ASC',
      },
      '#EXPORT_FILE_NAME#': '',
      '#EXPORT_FILE_TYPE#': 'xlsx',
      '#EXPORT_SHEET_NAME#': 'sys_message_structure',
    },
    {
      '#EXPORT_WHERE#': {
        TEMPLATE_ID: 'sys_messageid',
        FIELD_ID: 'MESSAGE_ID,MESSAGE_DESC,ACTIVE_FLAG',
      },
      '#EXPORT_SQL_ID#': 'apim.common.temp.export',
      '#EXPORT_API_NAME#': 'apim.common.temp.export',
      '#QUERY_SORT#': {
        SHOW_SEQUENCE: 'ASC',
      },
      '#EXPORT_FILE_NAME#': '',
      '#EXPORT_FILE_TYPE#': 'xlsx',
      '#EXPORT_SHEET_NAME#': 'sys_messageid',
    },
    {
      '#EXPORT_WHERE#': {
        TEMPLATE_ID: 'sys_rule_message',
        FIELD_ID: 'MESSAGE_GROUP_ID,MESSAGE_ID,ACTIVE_FLAG,RULE_ID',
      },
      '#EXPORT_SQL_ID#': 'apim.common.temp.export',
      '#EXPORT_API_NAME#': 'apim.common.temp.export',
      '#QUERY_SORT#': {
        SHOW_SEQUENCE: 'ASC',
      },
      '#EXPORT_FILE_NAME#': '',
      '#EXPORT_FILE_TYPE#': 'xlsx',
      '#EXPORT_SHEET_NAME#': 'sys_rule_message',
    },
    {
      '#EXPORT_WHERE#': {
        TEMPLATE_ID: 'sys_send_std_table',
        FIELD_ID:
          'MESSAGE_GROUP_ID,MESSAGE_ID,ACTIVE_FLAG,MESSAGE_TYPE,CHILDREN_TYPE,SELECT_SQL,KEY_FIELD,FLAG_FIELD,LOCAL_KEY_FIELD,RETURN_VALUES,PRE_SP,WEB_RETURN_FIELDS,WEB_RETURN_SUCCE',
      },
      '#EXPORT_SQL_ID#': 'apim.common.temp.export',
      '#EXPORT_API_NAME#': 'apim.common.temp.export',
      '#QUERY_SORT#': {
        SHOW_SEQUENCE: 'ASC',
      },
      '#EXPORT_FILE_NAME#': '',
      '#EXPORT_FILE_TYPE#': 'xlsx',
      '#EXPORT_SHEET_NAME#': 'sys_send_std_table',
    },
    {
      '#EXPORT_WHERE#': {
        TEMPLATE_ID: 'sys_std_table',
        FIELD_ID: 'MESSAGE_GROUP_ID,MESSAGE_ID,ACTIVE_FLAG,MESSAGE_TYPE,CHILDREN_TYPE,WEB_TABLE_NAME,STD_TABLE_NAME',
      },
      '#EXPORT_SQL_ID#': 'apim.common.temp.export',
      '#EXPORT_API_NAME#': 'apim.common.temp.export',
      '#QUERY_SORT#': {
        SHOW_SEQUENCE: 'ASC',
      },
      '#EXPORT_FILE_NAME#': '',
      '#EXPORT_FILE_TYPE#': 'xlsx',
      '#EXPORT_SHEET_NAME#': 'sys_std_table',
    },
  ]
  export default {
    name: 'Upload',
    props: {
      dataDetection: {
        type: Boolean,
        default: false,
      },
      isModal: {
        type: Boolean,
        default: false,
      },
      exportForm: {
        type: Object,
        default: () => {},
      },
      templateMethod: {
        type: String,
        default: 'service.temp.export',
      },
      uploadOptions: {
        type: Object,
        default: () => {},
      },
      tableColumn: {
        type: Array,
        default: () => [],
      },
    },
    emits: ['closeUpload'],
    data() {
      return {
        active: 1,
        templateForm: {
          '#EXPORT_WHERE#': {
            SUB_SYSTEM: '',
            TABLE_NAME: '',
            FIELD_NAME: '',
          },
          '#EXPORT_SQL_ID#': '',
          '#EXPORT_API_NAME#': '',
          '#QUERY_SORT#': { SHOW_SEQUENCE: 'ASC' },
          '#EXPORT_FILE_NAME#': '',
          '#EXPORT_FILE_TYPE#': 'xlsx',
          '#EXPORT_SHEET_NAME#': '',
        },
      }
    },
    computed: {
      uploadModal() {
        return this.isModal
      },
    },
    methods: {
      translateTitle,
      dialogClose() {
        this.$emit('closeUpload', false)
      },
      uploadSuccess(file) {
        let res = file.fileList[0].response
        if (res.code === '00000') {
          this.$baseMessage(res.msg, 'success')
          this.$emit('closeUpload', false)
        } else {
          this.$message({
            type: 'error',
            message: res.msg,
          })
        }
      },
      clickUpload() {
        if (this.$refs['fileUploadRef'].allImgNum) {
          this.$refs['fileUploadRef'].submitUpload()
        } else {
          this.$message({
            type: 'error',
            message: translateTitle('Dams.ApplicationInterface.请选择文件'),
          })
        }
      },
      async getRequest(api, p, method) {
        let res = await download(`${configSetting.domain}${api}`, {
          method: method,
          data: JSON.stringify(p),
        })
        let blob = new Blob([res], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
        let reader = new FileReader()
        reader.readAsDataURL(blob) // 转换为base64，可以直接放入a标签href
        reader.onload = (e) => {
          let a = document.createElement('a')
          a.download = translateTitle('Dams.ApplicationInterface.模板') + '.xlsx'
          a.href = e.target.result
          a.click()
        }
      },
      async download() {
        console.log(this.exportForm)
        let arr = [
          'sys_message_config',
          'sys_message_config_value',
          'sys_message_field_mapping',
          'sys_message_field_xml',
          'sys_messageid',
          'sys_message_sql',
          'sys_message_structure',
          'sys_rule_message',
          'sys_send_std_table',
          'sys_std_rule',
          'sys_std_table',
        ]
        if (this.exportForm.TABLE_NAME == 'sys_cust_org') {
          this.getRequest('/dams/dams-web', downloadList, 'apim.common.temp.export')
        } else if (arr.indexOf(this.exportForm.TABLE_NAME) > -1) {
          let list = []
          this.tableColumn.forEach((r, i) => {
            if (i != 0) {
              list.push(r.porp)
            }
          })
          if (this.exportForm.TABLE_NAME == 'sys_rule_message') {
            list.unshift('MESSAGE_GROUP_ID')
          }
          if (this.exportForm.TABLE_NAME == 'sys_message_config') {
            list.push('RETURN_CONDITION')
          }
          console.log(list)
          this.templateForm['#EXPORT_WHERE#'].FIELD_ID = list.join(',')
          this.templateForm['#EXPORT_WHERE#'].TEMPLATE_ID = this.exportForm.TABLE_NAME
          this.templateForm['#EXPORT_SQL_ID#'] = 'apim.common.temp.export'
          this.templateForm['#EXPORT_API_NAME#'] = 'apim.common.temp.export'
          this.getRequest('/dams/dams-web', this.templateForm, 'apim.common.temp.export')
        } else {
          this.templateForm['#EXPORT_WHERE#'].SUB_SYSTEM = this.exportForm.SUB_SYSTEM
          this.templateForm['#EXPORT_WHERE#'].TABLE_NAME = this.exportForm.TABLE_NAME
          this.templateForm['#EXPORT_SQL_ID#'] = this.templateMethod
          this.templateForm['#EXPORT_API_NAME#'] = this.templateMethod
          this.getRequest(configSetting.baseUrl, this.templateForm, this.templateMethod)
        }
      },
    },
  }
</script>
<style lang="scss" scoped>
  .footer_ {
    margin-top: 20px;
    text-align: center;
  }
  .flex_ {
    display: flex;
    justify-content: space-between;
  }
  .el-select .el-input .el-input__inner,
  .el-radio-group {
    width: 193px !important;
  }
  .hint_ {
    background: #fff5e6;
    padding: 15px 30px;
    box-sizing: border-box;
    .hint-title {
      font-weight: 600;
    }
    .hint-explain {
      margin-top: 10px;
    }
  }
  .step-two {
    margin-top: 20px;
  }
  .box_ {
    margin-top: 20px;
    display: flex;
    align-items: center;
    height: 115px;
    border: 1px solid #d8d3d3;
    p {
      margin-top: 10px;
    }
  }
  .left-box {
    background: #f5f5f5;
    width: 50px;
    height: 100%;
  }
  .abnormal-hint {
    padding: 15px;
    margin-top: 20px;
    box-sizing: border-box;
    border: 1px solid #f5f5f5;
    p {
      margin-top: 10px;
    }
  }
  .success {
    display: flex;
    justify-content: center;
    align-items: center;
    & > div {
      height: 300px;
    }
  }
</style>
